/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.servlet.http;

import javax.servlet.ServletRequestWrapper;
import java.util.Enumeration;

/**
 *  * HttpServletRequestWrapper提供了一个简便实现类，在对HttpServletRequest有改动需求的时，可以重写此类，之后Request的操作都会经过重写的类.
 *
 *
 * Provides a convenient implementation of the HttpServletRequest interface that
 * can be subclassed by developers wishing to adapt the request to a Servlet.
 * This class implements the Wrapper or Decorator pattern. Methods default to
 * calling through to the wrapped request object.
 * 
 *
 * @see 	HttpServletRequest
  * @since	v 2.3
 *
 */


public class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequest {

	/** 
	* Constructs a request object wrapping the given request.
	* @throws IllegalArgumentException if the request is null
	*/
    public HttpServletRequestWrapper(HttpServletRequest request) {
	    super(request);
    }
    
    private HttpServletRequest _getHttpServletRequest() {
	return (HttpServletRequest) super.getRequest();
    }

    /**
     * 返回认证类型.
     * 所有Servlet容器都支持basic、form、client_cert，digest不一定支持.
     * 若不支持，则返回null.
     *
     * The default behavior of this method is to return getAuthType()
     * on the wrapped request object.
     */

    public String getAuthType() {
	return this._getHttpServletRequest().getAuthType();
    }
   
    /**
     * 获取请求中带有的Cookie信息.
     * The default behavior of this method is to return getCookies()
     * on the wrapped request object.
     */
    public Cookie[] getCookies() {
	return this._getHttpServletRequest().getCookies();
    }

    /**
     * 以长整数形式返回一个特定的请求头，该长整数代表一个Date对象.
     * 该方法可以用在包含时间信息的header中，如：If-Modified-Since.
     *
     * The default behavior of this method is to return getDateHeader(String name)
     * on the wrapped request object.
     */
    public long getDateHeader(String name) {
	return this._getHttpServletRequest().getDateHeader(name);
    }
        	
    /**
     * 根据指定的头名称获取头的值.
     *      若存在多个，则返回第一个.
     *
     * The default behavior of this method is to return getHeader(String name)
     * on the wrapped request object.
     */
    public String getHeader(String name) {
	return this._getHttpServletRequest().getHeader(name);
    }
    
    /**
     * 根据指定的头名称获取头值的枚举.
     *      若没有找到，则返回空的枚举.
     *
     * The default behavior of this method is to return getHeaders(String name)
     * on the wrapped request object.
     */
    public Enumeration getHeaders(String name) {
	return this._getHttpServletRequest().getHeaders(name);
    }  

    /**
     * 获取所有的头的枚举.
     *
     * The default behavior of this method is to return getHeaderNames()
     * on the wrapped request object.
     */
  
    public Enumeration getHeaderNames() {
	return this._getHttpServletRequest().getHeaderNames();
    }
    
    /**
     * 根据指定头名称获取int类型的值.若未找到则返回-1，如不是int类型，则会抛出NumberFormatException异常.
     *
     * The default behavior of this method is to return getIntHeader(String name)
     * on the wrapped request object.
     */

     public int getIntHeader(String name) {
	return this._getHttpServletRequest().getIntHeader(name);
    }
    
    /**
     * 获取HTTP方法，如：GET、POST、PUT等.
     *
     * The default behavior of this method is to return getMethod()
     * on the wrapped request object.
     */
    public String getMethod() {
	return this._getHttpServletRequest().getMethod();
    }
    
    /**
     * 官网解释：
     *  返回与客户端发出此请求时发送的URL相关联的任何额外路径信息.
     *  额外的路径信息跟随servlet路径，但位于查询字符串之前，并以"/"字符开头.
     * 例如：url-pattern配置为/demo/*，请求URL为http://localhost/Pro/demo/htm/index.html，则pathInfo为/htm/index.html.
     *
     * The default behavior of this method is to return getPathInfo()
     * on the wrapped request object.
     */
    public String getPathInfo() {
	return this._getHttpServletRequest().getPathInfo();
    }

    /**
     * 返回servlet名称之后、
     *      查询字符串之前的任何额外路径信息，并将其转换为实际路径.
     *      与转换的CGI变量PATH U的值相同
     *
     * The default behavior of this method is to return getPathTranslated()
     * on the wrapped request object.
     */

     public String getPathTranslated() {
	return this._getHttpServletRequest().getPathTranslated();
    }

    /**
     * 返回项目根路径.
     * 例如：url-pattern配置为/demo/*，请求URL为http://localhost/Pro/demo/htm/index.html，则contextPath为/demo.
     *
     * The default behavior of this method is to return getContextPath()
     * on the wrapped request object.
     */
    public String getContextPath() {
	return this._getHttpServletRequest().getContextPath();
    }
    
    /**
     * 获得请求中的查询字符串，例如a=1&b=2这样的格式.
     * The default behavior of this method is to return getQueryString()
     * on the wrapped request object.
     */
    public String getQueryString() {
	return this._getHttpServletRequest().getQueryString();
    }
    
    /**
     * 如果用户已经过验证，则返回发出此请求的用户的登录信息，如果用户未经过验证，则返回 null.
     * 用户名是否随每个后续请求发送取决于浏览器和验证类型,返回的值与 CGI变量REMOTE_USER的值相同.
     *
     * The default behavior of this method is to return getRemoteUser()
     * on the wrapped request object.
     */
    public String getRemoteUser() {
	return this._getHttpServletRequest().getRemoteUser();
    }
    
 
    /**
     * 返回一个 boolean值，指示指定的逻辑"角色"中是否包含经过验证的用户.
     * 角色和角色成员关系可使用部署描述符定义.
     * 如果用户没有经过验证，则该方法返回 false.
     * The default behavior of this method is to return isUserInRole(String role)
     * on the wrapped request object.
     */
    public boolean isUserInRole(String role) {
	return this._getHttpServletRequest().isUserInRole(role);
    }
    
    
    
    /**
     *  返回包含当前已经过验证的用户的名称的 java.security.Principal对象.
     *      如果用户没有经过验证，则该方法返回 null.
     * The default behavior of this method is to return getUserPrincipal()
     * on the wrapped request object.
     */
    public java.security.Principal getUserPrincipal() {
	return this._getHttpServletRequest().getUserPrincipal();
    }
    
   
    /**
     * 获取请求对应的sessionId.
     * The default behavior of this method is to return getRequestedSessionId()
     * on the wrapped request object.
     */
    public String getRequestedSessionId() {
	return this._getHttpServletRequest().getRequestedSessionId();
    }
    
    /**
     * 请求URL的相对地址，包括服务器路径，不包括查询参数.
     * The default behavior of this method is to return getRequestURI()
     * on the wrapped request object.
     */
    public String getRequestURI() {
	return this._getHttpServletRequest().getRequestURI();
    }
	/**
     * 请求的URL地址，包含协议、主机名、端口和服务器路径，但是不包括查询参数.
     * The default behavior of this method is to return getRequestURL()
     * on the wrapped request object.
     */
    public StringBuffer getRequestURL() {
	return this._getHttpServletRequest().getRequestURL();
    }
	
    
    /**
     * 官方解释：
     *  返回此请求的URL中调用servlet的部分.
     *  此路径以"/"字符开头，包含servlet名称或到servlet的路径，但不包含任何额外的路径信息或查询字符串.
     *  与CGI变量SCRIPT_NAME的值相同.
     * 其实真是的意思就是，在配置webx.xml或编程式配置时，配置了url-pattern，请求的URL与url-pattern的有效部分重合部分就是servletPath，
     * 也可以理解为url-pattern的有效部分就是servletPath.
     * 例如：url-pattern配置为/demo/*，请求URL为http://localhost/Pro/demo/htm/index.html，则servletPath为/demo.
     *
     * The default behavior of this method is to return getServletPath()
     * on the wrapped request object.
     */
    public String getServletPath() {
	return this._getHttpServletRequest().getServletPath();
    }
    
    
    /**
     * 获得请求对应的当前sesson.
     * 在没有session的情况下：
     *  若create=true，则新创建一个session.
     *  若create=false，则不创建session，返回null.
     * The default behavior of this method is to return getSession(boolean create)
     * on the wrapped request object.
     */
    public HttpSession getSession(boolean create) {
	return this._getHttpServletRequest().getSession(create);
    }
    
    /**
     * 获得请求对应的当前sesson.
     * 若没有session，则创建一个session.
     * The default behavior of this method is to return getSession()
     * on the wrapped request object.
     */
    public HttpSession getSession() {
	return this._getHttpServletRequest().getSession();
    }
    
    /**
     * 返回session是否有效.
     * The default behavior of this method is to return isRequestedSessionIdValid()
     * on the wrapped request object.
     */ 

    public boolean isRequestedSessionIdValid() {
	return this._getHttpServletRequest().isRequestedSessionIdValid();
    }
     
    
    /**
     * sessionId是否从Cookie中获得.
     * The default behavior of this method is to return isRequestedSessionIdFromCookie()
     * on the wrapped request object.
     */
    public boolean isRequestedSessionIdFromCookie() {
	return this._getHttpServletRequest().isRequestedSessionIdFromCookie();
    }
    
    /**
     * sessionId是否从URL中获得.
     * The default behavior of this method is to return isRequestedSessionIdFromURL()
     * on the wrapped request object.
     */ 
    public boolean isRequestedSessionIdFromURL() {
	return this._getHttpServletRequest().isRequestedSessionIdFromURL();
    }
    
    /**
     * sessionId是否从URL中获得.
     * The default behavior of this method is to return isRequestedSessionIdFromUrl()
     * on the wrapped request object.
     */
    public boolean isRequestedSessionIdFromUrl() {
	return this._getHttpServletRequest().isRequestedSessionIdFromUrl();
    }


    
}
